import { getParams } from '../utils';
export default {
  data() {
    return {
      smartCodeDecodeType: 'codeflowurl',
      smartCodeBtnInfo:{// 页面显示的扫一扫按钮的基础信息
        id: 'smartCodeScan',
        name: this.$i18n('common.barcode.search.saoyisao'),
        icon: 'xicon-disconnect1',
        color: '#FF4D4F'
      }
    }
  },
  created() {
    this.initSmartCode();
  },
  methods: {
    initSmartCode() {
      let scriptElement = document.createElement('script');
      scriptElement.async = true;
      // 动态引入致码sdk
      scriptElement.src = `${window._ctxPath}/common/smartCode/smart-code-sdk.js${window._staticSuffix}`;
      scriptElement.onload = () => {
        //如果门户开启了可以不初始化
        this.bindSmartCodeEvent();
      }
      document.body.appendChild(scriptElement);
    },

    initSmartCodeData(scanType) {
      if (window.smartCodeSdk) {
        window.smartCodeSdk.handlerData(scanType, {
          'historyDecodeType': this.smartCodeDecodeType,
          'validateParam4History': this.smartCodeValidateParam4History,
          'callback4History': this.smartCodeCallBack
        });
      }
    },

    bindSmartCodeEvent() {
      if (window.smartCodeSdk) {
        window.smartCodeSdk.handlerDeviceState({
          // 设备状态变化回调
          'onChangeState': this.onChangeStateOfSmartCode,
          // 扫码关闭回调
          'onCloseSmartCode': this.onCloseSmartCode,
          // 扫码开启回调
          'onOpenSmartCode': this.onOpenSmartCode
        })
      } else {
        console.log('smartCodeSdk undefined !');
      }
    },
    // 设备状态变化回调
    onChangeStateOfSmartCode(res){
      if (res.action == 'open') {
        this.smartCodeBtnInfo.icon = 'xicon-state-selected';
        this.smartCodeBtnInfo.color = '#61B109';
      } else {
        this.smartCodeBtnInfo.icon = 'xicon-disconnect1';
        this.smartCodeBtnInfo.color = '#FF4D4F';
      }
    },
    // 扫码关闭回调
    onCloseSmartCode(){
      // TODO
    },
    // 扫码开启回调
    onOpenSmartCode(){
      // TODO
    },
    // 显示扫一扫页面
    showSmartCode() {
      if (window.smartCodeSdk) {
        window.smartCodeSdk.showSmartCode();
        this.initSmartCodeData(2);
      } else {
        console.log('smartCodeSdk undefined !');
      }
    },
    smartCodeValidateParam4History(params) {
      const __callback = params.callback;
      const __jsonData = params.jsonData;
      const msg = "";
      const success = true;
      const extParams = {};
      extParams = this.getSmartCodeDecodeParam(__jsonData);
      __callback({
          success: success,
          msg: msg,
          params: extParams
      });
    },
    // 获取解码参数
    getSmartCodeDecodeParam(params) {
      return {};
    },
    // 扫码成功后回调
    smartCodeCallBack(reval) {
      // TODO
    },

    // 是否有显示扫一扫功能的权限
    isHaveSmartCodePermission() {
      const urlParams = getParams(window.location.search);
      if (urlParams.bpmMenu) {// BPM门户不显示扫一扫功能
        return false;
      }
      if (
        window._hasPlugin('barCode') &&
        window._currentUser.userRoles.indexOf('VjoinStaff') == -1 &&
        this.supportSmartCode !== false
      ) {
        return true;
      } else return false;
    }
  }
}

